/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 11:19:39 AM
 * To change this template use File | Settings | File Templates.
 */
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Boolexp, Reference, BoolexpDefinition, InternalSchemaErrorException,
//            Context, Schema

class ReferenceBoolexp extends Boolexp
{

    ReferenceBoolexp(Element element, Schema schema)
    {
        ref = new Reference(element, schema);
    }

    BoolexpDefinition resolve(Context context)
    {
        Definition definition = ref.resolve();
        if(!(definition instanceof BoolexpDefinition))
            throw new InternalSchemaErrorException();
        else
            return (BoolexpDefinition)definition;
    }

    byte evaluate(Context context)
    {
        if(context.containsDef(ref.ref))
        {
            return 1;
        } else
        {
            BoolexpDefinition boolexpdefinition = resolve(context);
            context.enterDef(ref.ref);
            byte byte0 = boolexpdefinition.exp.evaluateMemo(context);
            context.leaveDef(ref.ref);
            return byte0;
        }
    }

    boolean isMentioned(Context context)
    {
        if(context.containsDef(ref.ref))
        {
            return false;
        } else
        {
            BoolexpDefinition boolexpdefinition = resolve(context);
            context.enterDef(ref.ref);
            boolean flag = boolexpdefinition.exp.isMentioned(context);
            context.leaveDef(ref.ref);
            return flag;
        }
    }

    public Element toXML(Context context)
    {
        if(context.containsDef(ref.ref))
        {
            return new Element("and", "http://www.brics.dk/DSD/2.0");
        } else
        {
            BoolexpDefinition boolexpdefinition = resolve(context);
            Element element = new Element("boolexp", "http://www.brics.dk/DSD/2.0");
            element.setAttribute("ref", ref.orig_ref);
            return element;
        }
    }

    boolean containsThis(Context context)
    {
        if(!this_check_performed)
        {
            if(context.containsDef(ref.ref))
                return false;
            BoolexpDefinition boolexpdefinition = resolve(context);
            context.enterDef(ref.ref);
            exp_contains_this = boolexpdefinition.exp.containsThis(context);
            context.leaveDef(ref.ref);
            this_check_performed = true;
        }
        return exp_contains_this;
    }

    Reference ref;
    boolean this_check_performed;
    boolean exp_contains_this;
}

